from enum import Enum

class HttpStatus(Enum):
    """HTTP状态码枚举"""
    
    # 2xx 成功
    OK = (200, "请求成功")
    CREATED = (201, "已创建")
    ACCEPTED = (202, "已接受")
    NO_CONTENT = (204, "无内容")
    
    # 3xx 重定向
    MOVED_PERMANENTLY = (301, "永久移动")
    FOUND = (302, "临时移动")
    SEE_OTHER = (303, "查看其它地址")
    NOT_MODIFIED = (304, "未修改")
    TEMPORARY_REDIRECT = (307, "临时重定向")
    PERMANENT_REDIRECT = (308, "永久重定向")
    
    # 4xx 客户端错误
    BAD_REQUEST = (400, "错误的请求")
    UNAUTHORIZED = (401, "未授权")
    FORBIDDEN = (403, "禁止访问")
    NOT_FOUND = (404, "未找到")
    METHOD_NOT_ALLOWED = (405, "方法不允许")
    NOT_ACCEPTABLE = (406, "不可接受")
    REQUEST_TIMEOUT = (408, "请求超时")
    CONFLICT = (409, "冲突")
    GONE = (410, "已删除")
    UNSUPPORTED_MEDIA_TYPE = (415, "不支持的媒体类型")
    TOO_MANY_REQUESTS = (429, "请求过多")
    
    # 5xx 服务器错误
    INTERNAL_SERVER_ERROR = (500, "服务器内部错误")
    NOT_IMPLEMENTED = (501, "未实现")
    BAD_GATEWAY = (502, "错误的网关")
    SERVICE_UNAVAILABLE = (503, "服务不可用")
    GATEWAY_TIMEOUT = (504, "网关超时")
    HTTP_VERSION_NOT_SUPPORTED = (505, "HTTP版本不支持")
    INSUFFICIENT_STORAGE = (507, "存储空间不足")

    def __init__(self, code, description):
        self.code = code
        self.description = description

    @property
    def value(self):
        """获取状态码数值"""
        return self.code

    @property
    def is_success(self):
        """是否是成功状态码（2xx）"""
        return 200 <= self.code < 300
    
    @property
    def is_redirect(self):
        """是否是重定向状态码（3xx）"""
        return 300 <= self.code < 400
    
    @property
    def is_client_error(self):
        """是否是客户端错误状态码（4xx）"""
        return 400 <= self.code < 500
    
    @property
    def is_server_error(self):
        """是否是服务器错误状态码（5xx）"""
        return self.code >= 500

    @classmethod
    def get_by_code(cls, code):
        """根据状态码获取枚举实例"""
        for status in cls:
            if status.code == code:
                return status
        raise ValueError(f"未知的状态码: {code}")

    def __eq__(self, other):
        """支持与数字直接比较"""
        if isinstance(other, int):
            return self.code == other
        return super().__eq__(other)

    def __int__(self):
        """支持转换为整数"""
        return self.code 